package com.cardbaobao.cardbabyclient.utils;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.model.BankNet;

import java.util.ArrayList;
import java.util.List;

/**
 * 导航工具类
 * Created by zad
 * 2017/1/18 11:13
 */

public class NavUtil {


    /**
     * 是否安装了某应用
     * @param context context
     * @param packageName 应用包名
     */
    public static boolean available(Context context, String packageName) {
        final PackageManager packageManager = context.getPackageManager();
        //获取所有已安装程序的包信息
        List<PackageInfo> infoList = packageManager.getInstalledPackages(0);
        //用于存储所有已安装程序的包名
        List<String> packageNames = new ArrayList<>();
        if (infoList != null) {
            for (int i = 0; i < infoList.size(); i++) {
                String packName = infoList.get(i).packageName;
                packageNames.add(packName);
            }
        }
        //判断packageNames中是否有目标程序的包名，有TRUE，没有FALSE
        return packageNames.contains(packageName);
    }

    /**
     * 开启百度导航
     *
     * @param context context
     * @param bankNet 银行网点
     */
    public static void toBaiDuNav(Context context, BankNet bankNet) {
        Intent intent;
        try {
            if (available(context, "com.baidu.BaiduMap")) {//传入指定应用包名
                intent = new Intent();
                // 驾车路线规划
                intent.setData(Uri.parse("baidumap://map/direction?destination=latlng:" + bankNet.getLatitude() + "," + bankNet.getLongitude() + "|name:" + bankNet.getAddress() + "&mode=driving"));
                context.startActivity(intent);
            } else {//未安装
                //market为路径，id为包名
                //显示手机上所有的market商店
                ToastUtil.showToastOnScreenCenter("您尚未安装百度地图");
                Uri uri = Uri.parse("market://details?id=com.baidu.BaiduMap");
                intent = new Intent(Intent.ACTION_VIEW, uri);
                context.startActivity(intent);
            }
        } catch (Exception e) {

        }
    }

    /**
     * 开启高德导航
     *
     * @param context context
     * @param bankNet 银行网点
     */
    public static void toAutoNav(Context context, BankNet bankNet) {
        Intent intent;
        try {
            if (available(context, "com.autonavi.minimap")) {
                intent = new Intent();
                intent.setData(Uri.parse("androidamap://navi?sourceApplication=" + context.getString(R.string.app_name) + "&poiname=" + bankNet.getAddress() + "&lat=" + bankNet.getLatitude() +
                        "&lon=" + bankNet.getLongitude() + "&dev=0"));
                context.startActivity(intent);
            } else {
                ToastUtil.showToastOnScreenCenter("您尚未安装高德地图");
                Uri uri = Uri.parse("market://details?id=com.autonavi.minimap");
                intent = new Intent(Intent.ACTION_VIEW, uri);
                context.startActivity(intent);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
